/*
//  Copyright © 2014 Allen County Public Library.

  //        ACPL Mobile is free software: This program is distributed and you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  ACPL Mobile is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, including without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ACPL Mobile. If not, see http://www.gnu.org/licenses/.
 * Author: James Harpe
 * 
 * 
 * Description: This activity displays a list of all library locations. If the user's device, has location services
 * 				enabled, the list is ordered by proximity to current location. 
 */

package us.in.lib.acpl.Mobile_ACPL;

import java.util.ArrayList;
import java.util.Collections;

import com.google.analytics.tracking.android.EasyTracker;

import us.in.lib.acpl.Mobile_ACPL.adapters.LocationAdapter;
import us.in.lib.acpl.Mobile_ACPL.context.Mobile_ACPL;
import us.in.lib.acpl.Mobile_ACPL.location.LocationInfo;
import us.in.lib.acpl.Mobile_ACPL.location.LocationListItem;
import us.in.lib.acpl.Mobile_ACPL.parents.BaseListActivity;
import us.in.lib.acpl.Mobile_ACPL.util.NetworkUtility;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class LocationActivity extends BaseListActivity {

	ArrayList<LocationListItem> locs = new ArrayList<LocationListItem>();

	LocationManager man;
	LocationListener listen;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		this.getActionBar().setTitle("  Locations & Hours");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location);

		man = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		if (man.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
				&& NetworkUtility.isOnline(this)) {
			listen = new MyLocationListener();
			man.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
					listen);

		} else {
			setLocation(null);
		}

		getListView().setOnItemClickListener(new LocationListListener());

	}

	  @Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance().activityStart(this); // Add this method.
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    EasyTracker.getInstance().activityStop(this); // Add this method.
	  }
	public void setLocation(Location loc) {
		createOrderedList(loc);
		findViewById(R.id.locationBar).setVisibility(View.GONE);
		setListAdapter(new LocationAdapter(this, locs));

		if (loc != null) {
			man.removeUpdates(listen);
		}
	}

	public void createOrderedList(Location loc) {
		locs = new ArrayList<LocationListItem>();
        //ACPL: Obviously these are ACPL specific locations
		locs.add(new LocationListItem("Main", LocationInfo.locDistanceInMiles(
				loc, "Main")));
		locs.add(new LocationListItem("Aboite", LocationInfo
				.locDistanceInMiles(loc, "Aboite")));
		locs.add(new LocationListItem("Dupont", LocationInfo
				.locDistanceInMiles(loc, "Dupont")));
		locs.add(new LocationListItem("Georgetown", LocationInfo
				.locDistanceInMiles(loc, "Georgetown")));
		locs.add(new LocationListItem("Grabill", LocationInfo
				.locDistanceInMiles(loc, "Grabill")));
		locs.add(new LocationListItem("Hessen Cassel", LocationInfo
				.locDistanceInMiles(loc, "Hessen Cassel")));
		locs.add(new LocationListItem("Little Turtle", LocationInfo
				.locDistanceInMiles(loc, "Little Turtle")));
		locs.add(new LocationListItem("Monroeville", LocationInfo
				.locDistanceInMiles(loc, "Monroeville")));
		locs.add(new LocationListItem("New Haven", LocationInfo
				.locDistanceInMiles(loc, "New Haven")));
		locs.add(new LocationListItem("Pontiac", LocationInfo
				.locDistanceInMiles(loc, "Pontiac")));
		locs.add(new LocationListItem("Shawnee", LocationInfo
				.locDistanceInMiles(loc, "Shawnee")));
		locs.add(new LocationListItem("Tecumseh", LocationInfo
				.locDistanceInMiles(loc, "Tecumseh")));
		locs.add(new LocationListItem("Waynedale", LocationInfo
				.locDistanceInMiles(loc, "Waynedale")));
		locs.add(new LocationListItem("Woodburn", LocationInfo
				.locDistanceInMiles(loc, "Woodburn")));

		Collections.sort(locs);
	}

	private class LocationListListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent(parent.getContext(),
					LocationDetailActivity.class);

			TextView v = (TextView) view.findViewById(R.id.location);
			intent.putExtra("location", v.getText());
			startActivity(intent);

		}

	}

	private class MyLocationListener implements LocationListener {

		@Override
		public void onLocationChanged(Location loc) {
			setLocation(loc);
		}

		@Override
		public void onProviderDisabled(String arg0) {

		}

		@Override
		public void onProviderEnabled(String arg0) {

		}

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) {

		}

	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  super.onConfigurationChanged(newConfig);
	  setContentView(R.layout.location);
	}
	


}
